﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VisualCode.Client;

namespace VisualCode.IDE
{
    public partial class MainWin : Form
    {
        private class ImageListConstants
        {
            public const string FolderOpen = "FolderOpen";
            public const string FolderClosed = "FolderClosed";
            public const string PublicClass = "PublicClass";
            public const string PublicField = "PublicField";
            public const string Reference = "Reference";
            public const string Namespace = "Namespace";
        }

        private VisualSolution _Solution;
        

        public MainWin()
        {
            InitializeComponent();
        }

        private void BuildSolution()
        {
            if (_Solution != null)
            {

                OutputTextBox.AppendText("--- Building Solution " + _Solution.Name + " ---" + Environment.NewLine);

                foreach (VisualProject project in _Solution.Projects)
                {
                    OutputTextBox.AppendText("--- Building project " + project.Name + " ---" + Environment.NewLine);

                    using (VisualCompiler compiler = new VisualCompiler(project))
                    {
                        compiler.Compile();
                    }
                }

                OutputTextBox.AppendText("--- Build Complete ---" + Environment.NewLine);
            }
        }

        private void NewSolution()
        {
            _Solution = new VisualSolution();
            _Solution.Name = "Solution1";

            VisualProject project = new VisualProject();
            project.Name = "Project1";

            _Solution.Projects.Add(project);

            DisplaySolution();
        }

        private void DisplaySolution()
        {
            SolutionExplorerTreeView.Nodes.Clear();

            if (_Solution != null)
            {
                SolutionExplorerTreeView.Nodes.Add(CreateSolutionNode(_Solution));
            }
        }

        private TreeNode CreateSolutionNode(VisualSolution solution)
        {
            TreeNode solutionNode = new TreeNode(_Solution.Name);
            solutionNode.ImageKey = ImageListConstants.FolderOpen;
            solutionNode.SelectedImageKey = ImageListConstants.FolderOpen;
            solutionNode.ContextMenuStrip = SolutionContextMenu;

            foreach (VisualProject project in solution.Projects)
            {
                solutionNode.Nodes.Add(CreateProjectNode(project));
            }

            solutionNode.ContextMenuStrip = SolutionContextMenu;

            return solutionNode;
        }

        private TreeNode CreateProjectNode(VisualProject project)
        {
            TreeNode projectNode = new TreeNode(project.Name);
            projectNode.SelectedImageKey = ImageListConstants.FolderOpen;
            projectNode.ImageKey = ImageListConstants.FolderOpen;
            projectNode.ContextMenuStrip = ProjectContextMenu;

            TreeNode referencesNode = new TreeNode("References");
            referencesNode.SelectedImageKey = ImageListConstants.FolderOpen;
            referencesNode.ImageKey = ImageListConstants.FolderOpen;

            //TODO: Add the references

            projectNode.Nodes.Add(referencesNode);

            //TreeNode classesNode = new TreeNode("Classes");
            //classesNode.SelectedImageKey = ImageListConstants.FolderOpen;
            //classesNode.ImageKey = ImageListConstants.FolderOpen;

            //projectNode.Nodes.Add(classesNode);

            foreach (VisualNamespace @namespace in project.Namespaces)
            {
                projectNode.Nodes.Add(CreateNamespaceNode(@namespace));
            }

            projectNode.ContextMenuStrip = ProjectContextMenu;

            return projectNode;
        }

        private TreeNode CreateNamespaceNode(VisualNamespace @namespace)
        {
            TreeNode namespaceNode = new TreeNode(@namespace.Name);

            namespaceNode.SelectedImageKey = ImageListConstants.Namespace;
            namespaceNode.ImageKey = ImageListConstants.Namespace;

            namespaceNode.ContextMenuStrip = NamespaceContextMenu;

            return namespaceNode;
        }

        private TreeNode CreateClassNode(VisualClass @class)
        {
            TreeNode classNode = new TreeNode(@class.Name);

            classNode.SelectedImageKey = ImageListConstants.PublicClass;
            classNode.ImageKey = ImageListConstants.PublicClass;

            classNode.ContextMenuStrip = ClassContextMenu;

            return classNode;
        }

        private void Import()
        {
        }

        private void Export()
        {
        }

        private void Exit()
        {
            this.Close();
        }

        private void buildSolutionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BuildSolution();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewSolution();
        }

        private void NewSolutionMenuItem_Click(object sender, EventArgs e)
        {
            NewSolution();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Exit();
        }

        private void MainWin_Load(object sender, EventArgs e)
        {

        }
    }
}
